/*        This file is part of P5ovray.
        
        P5ovray is free software: you can redistribute it and/or modify
        it under the terms of the GNU Lesser General Public License as 
        published by the Free Software Foundation, either version 3 of 
        the License, or (at your option) any later version.
        
        P5ovray is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU Lesser General Public License for more details.
        
        You should have received a copy of the GNU Lesser General Public
        License along with P5ovray.  If not, see <http://www.gnu.org/licenses/>.
*/

package zombie.povray;

import java.io.DataInputStream;
import java.io.FileInputStream;

import processing.core.PApplet;
import processing.core.PImage;

public class P5ovImageLoader {

	PImage img;
	byte[] fileHolder;
	
	public P5ovImageLoader (PImage _img, byte[] _fileHolder)
	{
		img = _img;
		fileHolder = _fileHolder;
	}
	
	private boolean readBytes ( DataInputStream stream, int[] array)
	{
		try {
			
			for (int i = 0; i < array.length; i++){
				array[i] = stream.readUnsignedByte();
			}
			
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return true;
		}
		return false;
	}
		
	private boolean loadArray(FileInputStream stream, byte[] array){
		try{
			stream.read(array);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return true;
		}
		return false;
	}
	
	private int signedSwitch(byte input)
	{
		//int output = input;//((input & 0xF) << 4) | ((input & 0xF0) >> 4);
		int output = 16 * ((input & 0xf0) >> 4) + (input & 0xF);
		return output;
	}
	
	public PImage loadTGAFromFile(String fileName)
	{
		FileInputStream file;
		try {
			file = new FileInputStream( fileName );
		}
		catch (Exception e)
		{
			PApplet.println("Error: Image with filename: " + fileName + " not found.");
			return img;
		}
		
		DataInputStream dataStream = new DataInputStream(file);
		
		int[] imgHeader = new int[18];
		
		if (readBytes(dataStream, imgHeader)) return img;
		
		int[] idField = new int[imgHeader[0x0]];
		if (readBytes(dataStream, idField)) return img;
		
		int[] colourMap = new int[(imgHeader[0x1] * imgHeader[0x5]) * imgHeader[0x7]];
		if (readBytes(dataStream, colourMap)) return img;
		
		if (imgHeader[2] == 2){
			
			int bpp = imgHeader[0x10];
			int width = (imgHeader[0xD] << 8) | imgHeader[0xC];
			int height = (imgHeader[0xF] << 8) | imgHeader[0xE];
			
			//System.out.println("Width = " + width + "\nHeight = " + height + "\n BPP = " + bpp);
			
			if (bpp == 24){
				if (fileHolder.length < (width * height * 3))
					fileHolder = new byte[(width * height * 3)];
				
				if (loadArray(file, fileHolder)) return img;
				
				for (int j = 0; j < (width * height); j++){
					if (img.pixels.length < (width * height)) return img;
					int i = j * 3;
					img.pixels[j] = (signedSwitch(fileHolder[i+2]) << 16) | (signedSwitch(fileHolder[i+1]) << 8) | signedSwitch(fileHolder[i]);
				}
				
			}
			
		}
		return img;
	}
	
}
